package com.yvtian.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yvtian.admin.pojo.SysCode;
import com.yvtian.admin.service.SysCodeService;
import com.yvtian.annotation.Log;
import com.yvtian.entity.Result;
import com.yvtian.entity.StatusCode;
import com.yvtian.enums.OperatorType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * (sys_code)表控制层
 *
 * @author long
 * @since 2024-04-11 13:44:03
 */
@RestController
@RequestMapping("sysCode")
public class SysCodeController {
    /**
     * 服务对象
     */
    @Resource
    private SysCodeService sysCodeService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public Result selectOne(@PathVariable Serializable id) {
        return new Result(true, "查询成功", StatusCode.OK, this.sysCodeService.getById(id));
    }

    @GetMapping(value = "/list")
    public Result findByPage(Page<SysCode> page, SysCode sysCode) {
        return new Result(true, "查询成功", StatusCode.OK, sysCodeService.findByPage(page, sysCode));
    }

    /**
     * 新增数据
     *
     * @param sysCode 实体对象
     * @return 新增结果
     */
    @Log(value = "添加数据", isSave = true, operType = OperatorType.INSERT)
    @PostMapping("insert")
    public Result insert(@RequestBody SysCode sysCode) {
        LambdaQueryWrapper<SysCode> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysCode::getTableName, sysCode.getTableName());
        int count = sysCodeService.count(lambdaQueryWrapper);
        if (count > 0) {
            return new Result(false, "表名重复", StatusCode.COD_EREPEAT_ERR);
        }
        sysCode.setCreateTime(new Date());
        return new Result(true, "插入成功", StatusCode.OK, this.sysCodeService.save(sysCode));
    }

    /**
     * 修改数据
     *
     * @param sysCode 实体对象
     * @return 修改结果
     */
    @PostMapping("update")
    public Result update(@RequestBody SysCode sysCode) {
        sysCode.setUpdateTime(new Date());
        return new Result(true, "更新成功", StatusCode.OK, this.sysCodeService.updateById(sysCode));
    }

    /**
     * 删除数据
     *
     * @param idMap 主键集合
     * @return 删除结果
     */
    @PostMapping("/del")
    public Result delete(@RequestBody Map<String, ArrayList<Integer>> idMap) {
        ArrayList<Integer> ids = idMap.get("ids");
        LambdaUpdateWrapper<SysCode> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.in(SysCode::getId, ids);
        lambdaUpdateWrapper.set(SysCode::getIsDel, 1);
        return new Result(true, "更新成功", StatusCode.OK, sysCodeService.update(lambdaUpdateWrapper));
    }

    @GetMapping("getCode/{name}")
    public Result getCode(@PathVariable String name) {
        return new Result(true, "获取成功", StatusCode.OK, sysCodeService.getCode(name));
    }
}
